﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace AcctrueWMS.Foundation.WebControls
{
    public class AccDropDownList : AccControl
    {
        public override string Render()
        {
            StringBuilder sb = new StringBuilder();
            #region 生成控件
            // sb.Append(GenerateDescription());
            sb.Append("<nobr>");
            sb.Append("<select  z-index='0' ");
            sb.Append(GenerateBasicProperty());
            if (this.Musted)
            {
                Style = "font-size:	9pt;width:90%;height:20px;border:1px solid #7b9ebd;";
                if (!string.IsNullOrEmpty(Style))
                    sb.Append(string.Format("style=\"{0}\"", Style));
                sb.Append(" >");
                sb.Append(GetOptions());
                sb.Append("</select>");

                sb.Append("<font style='color:red'>*</font>");
            }
            else
            {
                Style = "font-size:	9pt;width:90%;height:20px;border:1px solid #7b9ebd;";
                if (!string.IsNullOrEmpty(Style))
                    sb.Append(string.Format("style=\"{0}\"", Style));
                sb.Append(" >");
                sb.Append(GetOptions());
                sb.Append("</select>");
            }
            sb.Append("</nobr>");
            #endregion


            return sb.ToString();
        }


        private string GetOptions()
        {
            StringBuilder sb = new StringBuilder();

            if (base.DataSource is DataTable)
            {
                DataTable dtOptions = base.DataSource as DataTable;
                sb.Append("<option value=''>请选择</option>");
                foreach (DataRow row in dtOptions.Rows)
                {
                    sb.AppendFormat("<option value=\"{1}\" {2} >{0}</option>", new object[] {
                    row[base.ValueMember],
                    row[base.KeyMember],
                    row[base.KeyMember].ToString() == GetControlValue() ? "selected=\"selected\"" : ""
                });
                }
            }
            else if (base.DataSource is Dictionary<string, string>)
            {
                Dictionary<string, string> dic = base.DataSource as Dictionary<string, string>;
                foreach (KeyValuePair<string, string> kev in dic)
                {
                    sb.AppendFormat("<option value=\"{1}\" {2} >{0}</option>", new object[] {
                    kev.Key,
                   kev.Value,
                   kev.Key.ToString() == GetControlValue() ? "selected=\"selected\"" : ""});
                }
            }

            return sb.ToString();
        }

    }
}
